package com.ruoyi.lipin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lipin.domain.bo.LpProductBo;
import com.ruoyi.lipin.domain.vo.LpProductVo;
import com.ruoyi.lipin.service.ILpProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商品信息
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lipin/product")
public class LpProductController extends BaseController {

    private final ILpProductService iLpProductService;

    /**
     * 查询商品信息列表
     */
    @SaCheckPermission("lipin:product:list")
    @GetMapping("/list")
    public TableDataInfo<LpProductVo> list(LpProductBo bo, PageQuery pageQuery) {
        return iLpProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品信息列表
     */
    @SaCheckPermission("lipin:product:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LpProductBo bo, HttpServletResponse response) {
        List<LpProductVo> list = iLpProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品信息", LpProductVo.class, response);
    }

    /**
     * 获取商品信息详细信息
     *
     * @param productId 主键
     */
    @SaCheckPermission("lipin:product:query")
    @GetMapping("/{productId}")
    public R<LpProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long productId) {
        return R.ok(iLpProductService.queryById(productId));
    }

    /**
     * 新增商品信息
     */
    @SaCheckPermission("lipin:product:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LpProductBo bo) {
        return toAjax(iLpProductService.insertByBo(bo));
    }

    /**
     * 修改商品信息
     */
    @SaCheckPermission("lipin:product:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LpProductBo bo) {
        return toAjax(iLpProductService.updateByBo(bo));
    }

    /**
     * 删除商品信息
     *
     * @param productIds 主键串
     */
    @SaCheckPermission("lipin:product:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] productIds) {
        return toAjax(iLpProductService.deleteWithValidByIds(Arrays.asList(productIds), true));
    }
}
